package com.example.cinema.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.example.cinema.BR;
import com.example.cinema.R;
import com.example.cinema.UserAttribute;
import com.example.cinema.adapter.CommentAdapter;
import com.example.cinema.bindingdata.ShortBindingData;
import com.example.cinema.bindingdata.UserVideoInfoData;
import com.example.cinema.bindingdata.VideoData;
import com.example.cinema.databinding.ActivityUservideoListBinding;
import com.example.cinema.request.RequestSend;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ActivityUserVideo extends AppCompatActivity {

    private ActivityUservideoListBinding AULB;
    private int p = 1;
    private int l = 15;
    private List<VideoData> videoData = new ArrayList<>();
    private UserVideoInfoData uvdata = new UserVideoInfoData();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AULB = DataBindingUtil.setContentView(this, R.layout.activity_uservideo_list);

        AULB.setUvdata(uvdata);
        init();
        setadapterlist();

        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }
    }

    private String usid;
    private UserAttribute attribute;

    private void init() {
        attribute = (UserAttribute) getApplication();
        Intent intent = getIntent();
        usid = intent.getStringExtra("uid");
        //滚动
        AULB.gridview.setOnScrollListener(new AbsListView.OnScrollListener() {
            private boolean aBoolean = false;
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState==0)
                {
                    if(aBoolean)
                    {
                        aBoolean= false;
                        p++;
                        ReqData();
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int t = firstVisibleItem + visibleItemCount;
                if (t >= totalItemCount) {
                    aBoolean = true;
                }
            }
        });
        //点击
        AULB.gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Gson gson = new Gson();
                String str = gson.toJson(videoData);
                String usd = gson.toJson(uvdata);
                Log.i("HHDSH",usd);
                Log.i("HHDSH",str);
                Intent intent1 = new Intent();
                intent1.putExtra("position", position);
                intent1.putExtra("data", str);
                intent1.putExtra("udata", usd);
                intent1.setAction("userplaylist");

                startActivity(intent1);
            }
        });
        //关注
        AULB.followss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = AULB.followss.getText().toString();
                if (s.equals("已关注")) {
                    return;
                }
                new Thread()
                {
                    @Override
                    public void run() {
                        RequestSend requestSend = new RequestSend(getApplicationContext());
                        Log.i("QQQQQQQQ", uvdata.usersid.get());
                        boolean b = requestSend.Follow(attribute.getUID(), uvdata.usersid.get(), attribute);
                        Log.i("QQQQQQQQ", "关注结果" + b);
                        Message message = new Message();
                        message.what = 1003;
                        message.obj = b;
                        handler.sendMessage(message);
                    }
                }.start();
            }
        });
        //返回
        AULB.finsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Isfollow();
    }

    //适配器
    private CommentAdapter adapter;

    public void setadapterlist() {
        adapter = new CommentAdapter(getApplicationContext(), videoData, R.layout.uservideo_item_view, BR.usvideodata);
        AULB.gridview.setAdapter(adapter);
        ReqData();
    }


    //获取数据
    private void ReqData() {
        new Thread() {
            @Override
            public void run() {
                JSONObject jo = new RequestSend(getApplicationContext()).GetUidList(usid, p, l);
                try {
                    JSONObject oo = jo.getJSONObject("info");
                    uvdata.userimg.set(oo.getString("userimg"));
                    uvdata.usersid.set(oo.getString("usersid"));
                    uvdata.userterm.set(oo.getString("useralias"));
                    uvdata.userfans.set(oo.getString("userfans"));
                    uvdata.worksnum.set(oo.getString("userworks"));
                    uvdata.follow.set(oo.getString("userfollow"));

                    Log.i("TTTTT", oo.getString("usersid"));
                    JSONArray array = jo.getJSONArray("data");
                    Log.i("TTTTT", array.length() + "");
                    List<VideoData> data = new ArrayList<>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject o = array.getJSONObject(i);
                        VideoData vdata = new VideoData();
                        vdata.videoimge.set(o.getString("videoimage"));
                        vdata.videoname.set(o.getString("videotitle"));
                        vdata.videourls.set(o.getString("videoplayurl"));
                        vdata.videoshort.set(o.getString("videoshort"));
                        vdata.videobrief.set(o.getString("videotexts"));
                        vdata.videotage.set(o.getString("videotag"));
                        vdata.videosid.set(o.getString("videovid"));
                        Log.i("VCCC", o.getString("videovid"));
                        data.add(vdata);
                    }
                    videoData.addAll(data);
                    Log.i("TTTTT", videoData.size() + "数量");
                    Log.i("TTTTT", videoData.get(0).videoimge.get());

                    handler.sendEmptyMessage(1000);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    //检测是否已关注
    private void Isfollow() {
        new Thread() {
            @Override
            public void run() {
                RequestSend requestSend = new RequestSend(getApplicationContext());
                boolean abool = requestSend.isFollow(attribute.getUID(), usid, attribute);
                Message message = new Message();
                message.what = 1001;
                message.obj = abool;
                handler.sendMessage(message);
            }
        }.start();
    }


    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1000) {
                adapter.notifyDataSetChanged();
                return;
            }
            if (msg.what == 1001) {
                AULB.followss.setText("已关注");
            } else {
                AULB.followss.setText("关注");
            }
            if (msg.what == 1002) {
                boolean b = (boolean) msg.obj;
                if (b) {
                    AULB.followss.setText("已关注");
                } else {
                    AULB.followss.setText("关注");
                }
            }
        }
    };
}
